import weka.classifiers.*;
import weka.classifiers.trees.J48;
import weka.core.*;

import java.io.*;
import java.util.*;

/**
 * Simulates a 10-fold cross-validation, but prints the evaluations for
 * each fold. Takes an ARFF file as first argument.
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 * @see Evaluation#crossValidateModel(Classifier,Instances,int,Random)
 * @see Evaluation#evaluateModel(Classifier,Instances)
 */
public class SeparateCVFolds {
  public static void main(String[] args) throws Exception {
    int seed = 1;
    int folds = 10;
    Classifier classifier = new J48();
    Random random = new Random(seed);
   
    // load  ARFF file
    Instances data = new Instances(new BufferedReader(new FileReader(args[0])));
    data.setClassIndex(data.numAttributes() - 1);
    data.randomize(random);
    if (data.classAttribute().isNominal()) {
      data.stratify(folds);
    }

    // emulate CV
    for (int i = 0; i < folds; i++) {
      Evaluation eval = new Evaluation(data);
      Instances train = data.trainCV(folds, i, random);
      eval.setPriors(train);
      Classifier copiedClassifier = Classifier.makeCopy(classifier);
      copiedClassifier.buildClassifier(train);
      Instances test = data.testCV(folds, i);
      eval.evaluateModel(copiedClassifier, test);
      // output fold statistics
      System.out.println("\nFold " + (i+1) + ":\n" + eval.toSummaryString());
    }

    // and finally a "normal" CV
    Evaluation eval = new Evaluation(data);
    eval.crossValidateModel(classifier, data, folds, new Random(seed));
    // output statistics
    System.out.println("\n10-fold CV:\n" + eval.toSummaryString());
  }
} 